package org.germinus.telcoblocks.configuration;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.util.Properties;
import java.util.logging.*;

public class XMLParser extends DefaultHandler {

    private final XMLReader xmlReader;
    
    private final Properties propiedades;
    
    private String entidad = "";

    public XMLParser() throws SAXException {
        xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler(this);
        xmlReader.setErrorHandler(this);
        propiedades = new Properties();
    }

    public Properties read(File archivoXML,String entidad)
             throws FileNotFoundException, IOException,
                       SAXException {
    	this.entidad = entidad;
        FileReader fr = new FileReader(archivoXML);
        xmlReader.parse(new InputSource(fr));
        return propiedades;
    }
    
    @Override
    public void startDocument() {
    }

    @Override
    public void endDocument() {
    }

    @Override
    public void startElement(String uri, String name,
              String qName, Attributes atts) {
    	if (name == entidad ){
    		for (int i = 0; i < atts.getLength(); i++){
    			propiedades.setProperty(atts.getLocalName(i), atts.getValue(i));    			
    		}
    	}        
    }

    @Override
    public void endElement(String uri, String name,
                                 String qName) {
    }
}

